package com.pavlinic.mylisp;

import java.util.HashMap;
import java.util.Map;

public class GlobalEvaluationContext implements EvaluationContext {
	private static GlobalEvaluationContext instance;

	public static GlobalEvaluationContext getInstance() {
		if (instance == null) {
			instance = new GlobalEvaluationContext();
		}
		return instance;
	}

	private Map<String, Expression> map = new HashMap<String, Expression>();

	public void bind(String bindingName, Expression boundExpression) {
		map.put(bindingName, boundExpression);
	}

	public Expression getBinding(String value) {
		return map.get(value);
	}

	public EvaluationContext withBinding(String value, Expression expression) {
		//Once we try to add extra bindings to this context, we are presumably in
		//a function call so we need to be immutable from here
		return new ImmutableEvaluationContext(map).withBinding(value, expression);
	}

	public static void clear() {
		instance = null;
	}

	public boolean hasBinding(String binding) {
		return map.containsKey(binding);
	}
}
